<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="kissdata">


    <meta name="subtitle" content="人生笔记">


    <meta name="description" content="南邮计算机学院2021毕业生王鹏的介绍，cs软件工程专业">


    <meta name="keywords" content="wangpeng 王鹏 南邮 南京">


<title>🆗 | Centos7 安装openCV4 | 王鹏的小岛</title>



    <link rel="icon" href="/myIcon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        <!-- MathJax配置，可通过单美元符号书写行内公式等 -->
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
    "HTML-CSS": {
        preferredFont: "TeX",
        availableFonts: ["STIX","TeX"],
        linebreaks: { automatic:true },
        EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
        inlineMath: [ ["$", "$"], ["\\(","\\)"] ],
        processEscapes: true,
        ignoreClass: "tex2jax_ignore|dno",
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
        equationNumbers: { autoNumber: "AMS" },
        noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } },
        Macros: { href: "{}" }
    },
    messageStyle: "none"
    });
</script>
<!-- 给MathJax元素添加has-jax class -->
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>
<!-- 通过连接CDN加载MathJax的js代码 -->
<script type="text/javascript" async
        src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML">
</script>


    


<meta name="generator" content="Hexo 7.0.0"></head>

<body>
    <script>
        // this function is used to check current theme before page loaded.
        (() => {
            const currentTheme = window.localStorage && window.localStorage.getItem('theme') || '';
            const isDark = currentTheme === 'dark';
            const pagebody = document.getElementsByTagName('body')[0]
            if (isDark) {
                pagebody.classList.add('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Dark"
            } else {
                pagebody.classList.remove('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Light"
            }
        })();
    </script>

    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">侠之大者</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/update/mystate">Career</a>
                
                    <a class="menu-item" href="/category">Legend</a>
                
                    <a class="menu-item" href="/about">Me</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">侠之大者</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/update/mystate">Career</a>
                
                    <a class="menu-item" href="/category">Legend</a>
                
                    <a class="menu-item" href="/about">Me</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
            <div class="main">
                <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    var tocbot_timer;
    var DEPTH_MAX = 6; // 为 6 时展开所有
    var tocbot_default_config = {
        tocSelector: '.tocbot-list',
        contentSelector: '.post-content',
        headingSelector: 'h1, h2, h3, h4, h5',
        orderedList: false,
        scrollSmooth: true,
        onClick: extend_click,
    };

    function extend_click() {
        clearTimeout(tocbot_timer);
        tocbot_timer = setTimeout(function() {
            tocbot.refresh(obj_merge(tocbot_default_config, {
                hasInnerContainers: true
            }));
        }, 420); // 这个值是由 tocbot 源码里定义的 scrollSmoothDuration 得来的
    }

    document.ready(function() {
        tocbot.init(obj_merge(tocbot_default_config, {
            collapseDepth: 1
        }));
    });

    function expand_toc() {
        var b = document.querySelector('.tocbot-toc-expand');
        var expanded = b.getAttribute('data-expanded');
        expanded ? b.removeAttribute('data-expanded') : b.setAttribute('data-expanded', true);
        tocbot.refresh(obj_merge(tocbot_default_config, {
            collapseDepth: expanded ? 1 : DEPTH_MAX
        }));
        b.innerText = expanded ? 'Expand all' : 'Collapse all';
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

    function obj_merge(target, source) {
        for (var item in source) {
            if (source.hasOwnProperty(item)) {
                target[item] = source[item];
            }
        }
        return target;
    }
</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">🆗 | Centos7 安装openCV4</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">kissdata</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">Nov 4, 2023&nbsp;&nbsp;0:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Deploy/">Deploy</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <p>环境：Centos7 + opencv4.8</p>
<p>需要命令工具：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">  tool       version</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">-- -- -- | -- -- -- <span class="comment">#</span></span></span><br><span class="line">· Go          1.21</span><br><span class="line">· Git         2.42</span><br><span class="line">· Cmake       3.28</span><br></pre></td></tr></table></figure>

<p>最好先安装 ffmpeg，再装这个，opencv里有对 ffmpeg 的支持配置。</p>
<p>【前提】</p>
<ol>
<li><p>先 <code>ls /usr/local/go</code> 确认go环境已经配置，没有需要安装。</p>
</li>
<li><p>确认cmake版本。goCV 的 Makefile会自动从mirror拉下载旧版本的cmake(2.8)，这个不能用（后面需要<strong>去除</strong>）。所以先配置好cmake，具体安装见 📗 <a target="_blank" rel="noopener" href="https://ae85.gitee.io/2023/11-1/tool_cmake/">CentOS7 安装最新版cmake</a></p>
</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cmake --version   # 确认原先版本不是旧版本</span><br></pre></td></tr></table></figure>

<h1 id="一、获取goVC"><a href="#一、获取goVC" class="headerlink" title="一、获取goVC"></a>一、获取goVC</h1><p>下载 goCV，修改Makefile</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">mkdir test &amp;&amp; cd test</span><br><span class="line">go mod init test</span><br><span class="line">go get -u -d gocv.io/x/gocv      # -d表示只下载不安装</span><br><span class="line">go install gocv.io/x/gocv@latest</span><br><span class="line">cd /root/go/pkg/mod/;ls          # /root/go 是GOPATH默认值</span><br><span class="line">cd gocv.io/x/gocv@*</span><br><span class="line">chmod 666 Makefile</span><br><span class="line">vim Makefile</span><br></pre></td></tr></table></figure>

<p>或者将Makefile保存到win上，要修改。不要一进来就 <code>make install</code> 安装，会下载不下来。</p>
<p><img src="/../image/knowledge_gocv/faqinstall1.png"></p>
<h1 id="二、修改Makefile"><a href="#二、修改Makefile" class="headerlink" title="二、修改Makefile"></a>二、修改Makefile</h1><p>【2.1】去除 git 和 cmake。将 20-22 行中的各自cmake去除</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">RPMS=curl wget gtk2-devel ...</span><br><span class="line"><span class="comment">#原先 RPMS=cmake curl wget git gtk2-devel ...</span></span><br><span class="line">DEBS=unzip wget build-essential curl ...</span><br><span class="line"><span class="comment">#原先 DEBS=unzip wget build-essential cmake curl git ...</span></span><br><span class="line">JETSON=build-essential unzip pkg-config ...</span><br><span class="line"><span class="comment">#原先 JETSON=build-essential cmake git unzip pkg-config ...</span></span><br></pre></td></tr></table></figure>

<p>主要是去除 cmake，不然 centOS7会下载旧的cmake，会把本来手动安装好了的新版替换了，影响后面编译。</p>
<p>【2.2】去除tbb-level。centOS7默认安装4.1版本，这个如果gcc是高版本的不支持。需要手动安装新的。</p>
<p>【2.3】去除opencv。opencv有92M，很可能下载不下来。<strong>先自己上传 opencv包</strong>，存放在 <code>/tmp/opencv</code> 目录</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cd /tmp   # 进入tmp目录</span><br><span class="line">mkdir opencv &amp;&amp; cd opencv</span><br><span class="line">rz        # 第一次上传可能出错，要删除再来一次</span><br><span class="line">rz        # 两个共 150+ M</span><br><span class="line">chmod 666 opencv*</span><br></pre></td></tr></table></figure>

<p>然后选择win上的这两个文件，两个文件一共 150+ M。然后<strong>将Makefile操作opencv的部分去除。</strong> 注意是在顶格加 <code>#</code> ，因为对其命令行前面的空格是用Tab键形成的。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">...... 61</span><br><span class="line"><span class="section">download:</span></span><br><span class="line"><span class="comment">#1	rm -rf $(TMP_DIR)opencv </span></span><br><span class="line"><span class="comment">#2	mkdir $(TMP_DIR)opencv</span></span><br><span class="line">	cd <span class="variable">$(TMP_DIR)</span>opencv</span><br><span class="line"><span class="comment">#3	curl -Lo opencv.zip https://github.com/opencv/opencv/archive/$(OPENCV_VERSION).zip</span></span><br><span class="line">	unzip -q opencv.zip</span><br><span class="line"><span class="comment">#4	curl -Lo opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/$(OPENCV_VERSION).zip</span></span><br><span class="line">	unzip -q opencv_contrib.zip</span><br><span class="line"><span class="comment">#5	rm opencv.zip opencv_contrib.zip</span></span><br><span class="line">	cd -</span><br><span class="line">...... 225	</span><br><span class="line"><span class="comment">#6	rm -rf $(TMP_DIR)opencv</span></span><br></pre></td></tr></table></figure>

<p>注意上传的包可能有<code>-4.8.x</code> 这样的后缀，原先 <code>curl</code> 时没有，要修改。或者全部删除自己预先解压，所以如果二次安装，在重新安装前，先把&#x2F;opencv目录里的两个解压出来的文件夹删除。</p>
<p><img src="/../image/knowledge_gocv/faqinstall2.png"></p>
<p>上面都做完后，现在才正式开始安装。对新的 Makefile（如果是下载到本地修改，用 <code>rz</code> 上传前别忘了先 <code>rm</code> 原来的 Makefile）：<code>make install</code></p>
<p>其实Makefile里下载其他模块的部分都不成功。提示 “Couldn’t connect to server”。</p>
<h1 id="三、配置opencv4-pc"><a href="#三、配置opencv4-pc" class="headerlink" title="三、配置opencv4.pc"></a>三、配置opencv4.pc</h1><p>前面最后安装完后，会提示</p>
<p><img src="/../image/knowledge_gocv/faqinstall3.png"></p>
<h2 id="3-1-增加环境变量"><a href="#3-1-增加环境变量" class="headerlink" title="3.1 增加环境变量"></a>3.1 增加环境变量</h2><p>通过 <code>vim /root/.bashrc</code> ，在最下方增加啊两行</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib64/pkgconfig</span><br><span class="line">export PKG_CONFIG_PATH</span><br></pre></td></tr></table></figure>

<p>source一下。</p>
<h2 id="3-2-加载opencv共享库"><a href="#3-2-加载opencv共享库" class="headerlink" title="3.2 加载opencv共享库"></a>3.2 加载opencv共享库</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">[root@~]# </span><span class="language-bash">find / -name opencv4.pc</span></span><br><span class="line">/tmp/opencv/opencv-4.6.0/build/unix-install/opencv4.pc</span><br><span class="line">/usr/local/lib64/pkgconfig/opencv4.pc     # 确认.pc文件存在</span><br><span class="line"><span class="meta prompt_">[root@~]# </span><span class="language-bash">ll /etc/ld.so.conf.d/     <span class="comment"># 查看是否opencv配置文件，默认没有</span></span></span><br><span class="line">...</span><br><span class="line"><span class="meta prompt_">[root@~]# </span><span class="language-bash">vim /etc/ld.so.conf.d/opencv.conf <span class="comment"># 输入下面一行</span></span></span><br><span class="line">/usr/local/lib64 </span><br><span class="line"><span class="meta prompt_">[root@~]# </span><span class="language-bash">ldconfig</span></span><br></pre></td></tr></table></figure>

<p>这里也可以 直接 <code>vim /etc/ld.so.conf</code> 直接将内容放到下一行。</p>
<p>运行结果：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost gocv@v0.31.0]<span class="comment"># source /root/.bashrc</span></span><br><span class="line">[root@localhost gocv@v0.31.0]<span class="comment"># go run ./cmd/version/main.go</span></span><br><span class="line">gocv version: 0.31.0</span><br><span class="line">opencv lib version: 4.6.0</span><br></pre></td></tr></table></figure>

<h1 id="附-参考"><a href="#附-参考" class="headerlink" title="附 参考"></a>附 参考</h1><p>官网 <a target="_blank" rel="noopener" href="https://gocv.io/getting-started/linux/">Linux :: GoCV - Using OpenCV 4</a></p>

        </div>

        
            <section class="post-copyright">
                
                
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>人生三大错觉：我躲得过去，我能反杀，是不是<strong>喜欢我?</strong></span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2023/11-22/tool_git/">⛵ 各种环境都要能用 Git</a>
            
            
            <a class="next" rel="next" href="/2023/10-30/knowledge_c/">C 可变长参数的应用</a>
            
        </section>


    </article>
</div>

            </div>
            <footer id="footer" class="footer">
    <div class="copyright">
        <span>187-9597-5517 &nbsp; 王鹏 @ <a href="https://cs.njupt.edu.cn/" target="_blank">njupt</a</span>
    </div>
</footer>

    </div>
</body>

</html>